* ======================================
* ----------- Network Family -----------
* ======================================


	
* Parents
* =======

	use $dta\ImportParents.dta, clear
	rename idParent idFamily
	gen Family = 1
	tempfile parents
	save `parents'

	
	
* Siblings
* ========

	use $dta\ImportSiblings.dta, clear
	rename idSibling idFamily
	gen Family = 2
	tempfile siblings
	save `siblings'

	
	
* Children
* ========

	use $dta\ImportChildren.dta, clear
	rename idChild idFamily
	gen Family = 3
	tempfile children
	save `children'
		


* Grandparents
* ============

	use $dta\ImportParents.dta, clear // Parents
	
	rename idPers idPers_tmp
	rename idParent idPers
	
	joinby idPers using "$dta\ImportParents.dta" // Parents of parents
	
	keep idParent idPers_tmp
	rename idPers_tmp idPers
	rename idParent idFamily
	gen Family = 4
	
	sort idPers idFamily
	by idPers idFamily: drop if _n > 1 // drop multiples

	tempfile grandparents
	save `grandparents'
	


* Grandchildren
* =============

	use $dta\ImportChildren.dta, clear 	// Children
	
	rename idPers idPers_tmp
	rename idChild idPers
	
	joinby idPers using "$dta\ImportChildren.dta" // Children of children
	
	keep idChild idPers_tmp
	rename idPers_tmp idPers
	rename idChild idFamily
	gen Family = 5

	sort idPers idFamily
	by idPers idFamily: drop if _n > 1 // drop multiples

	tempfile grandchildren
	save `grandchildren'

	
	
* Nieces and nephews
* ==================

	use $dta\ImportSiblings.dta, clear // Siblings
	
	rename idPers idPers_tmp
	rename idSibling idPers
	
	joinby idPers using "$dta\ImportChildren.dta" // Children of siblings
	
	keep idChild idPers_tmp
	rename idPers_tmp idPers
	rename idChild idFamily
	gen Family = 6
	
	sort idPers idFamily	
	by idPers idFamily: drop if _n > 1 // drop multiples	
	
	tempfile niecesnephews
	save `niecesnephews'
	
	
	
* AuntsUncles
* ===========

	use $dta\ImportParents.dta, clear // Parents
	
	rename idPers idPers_tmp
	rename idParent idPers
	
	joinby idPers using "$dta\ImportSiblings.dta" // Siblings of parents
	
	keep idSibling idPers_tmp
	rename idPers_tmp idPers
	rename idSibling idFamily
	gen Family = 7
	
	sort idPers idFamily
	by idPers idFamily: drop if _n > 1 // drop multiples

	tempfile auntsuncles
	save `auntsuncles'
	
	
	

* Cousins
* =======

	use `auntsuncles', clear // AuntsUncles
	
	rename idPers idPers_tmp
	rename idFamily idPers
	
	joinby idPers using "$dta\ImportChildren.dta" // Children of aunts uncles
	
	keep idChild idPers_tmp
	rename idPers_tmp idPers
	rename idChild idFamily
	gen Family = 8

	sort idPers idFamily
	by idPers idFamily: drop if _n > 1 // drop multiples

	tempfile cousins
	save `cousins'



* Append all family
* =================

	clear
	append using `parents'
	append using `siblings'
	append using `children'
	append using `grandparents'
	append using `grandchildren'
	append using `niecesnephews'
	append using `auntsuncles'
	append using `cousins'

	bysort idPers idFamily (Family): drop if _n > 1 // drop multiples
	
	label define famlab 1 "Parent" 2 "Sibling" 3 "Child" 4 "Grandparent" ///
		 5 "Grandchild" 6 "NieceNephew" 7 "AuntUncle" 8 "Cousin", replace
	label values Family famlab
	
	sort idPers Family idFamily
	
	
* Save network types
* ==================

	* Narrow
	preserve
	rename idFamily idFam1
	keep idPers idFam1 Family
	keep if Family >= 1 & Family <= 3
	save $dta\NetworkFam1.dta, replace
	restore
	
	* Broad
	preserve
	rename idFamily idFam2
	keep idPers idFam2 Family
	keep if Family >= 1 & Family <= 8
	save $dta\NetworkFam2.dta, replace
	restore
	
